home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-08-25 | 3.5 KB | 158 lines | [TEXT/CWIE] |
-
-
- #include "EditorUtilities.h"
-
- #include "CTextEditorWindow.h"
- #include "LStyledTextEdit.h"
-
- const ResType kTheTextET = 'Text';
- const ResType kRemoveTextB = 'rmvB';
-
-
-
- SInt32 DoEditText( SInt16 inFileRefNum, SInt16 &inTextRsrcID )
- {
- // Return values: 0 = Changes accepted, 1 = Canceled, no changes made, 2 = Removed, < 0 = Internal error, no changes mad
- SInt32 result = -1;
-
- SInt16 savedResFile = CurResFile();
- UseResFile( inFileRefNum );
-
- try
- {
- CTextEditorWindow* theEditorWindow = new CTextEditorWindow( inFileRefNum, inTextRsrcID );
-
- if( inTextRsrcID == 0 )
- theEditorWindow->mTextRsrcID = GetUniqueIDForResType( inFileRefNum, 'TEXT' );
-
- result = theEditorWindow->DoEdit();
-
- delete theEditorWindow;
-
- if( result == 0 || result == 2 )
- inTextRsrcID = theEditorWindow->mTextRsrcID;
-
- }
- catch( OSErr theErr )
- {
- ;
- }
-
- UseResFile( savedResFile );
-
- return result;
- }
-
-
- // ---------------------------------------------------------------------------
- // • CTextEditorWindow
- // ---------------------------------------------------------------------------
- CTextEditorWindow::CTextEditorWindow( SInt16 inFileRefNum, SInt16 inTextRsrcID )
- : StDialogHandler(145, NULL), mFileRefNum(inFileRefNum), mTextRsrcID(inTextRsrcID)
- {
- }
-
- // ---------------------------------------------------------------------------
- // • ~CTextEditorWindow
- // ---------------------------------------------------------------------------
- CTextEditorWindow::~CTextEditorWindow()
- {
- }
-
-
- SInt32 CTextEditorWindow::DoEdit()
- {
-
- LWindow *theDialog = GetDialog();
-
- this->LoadWindowFromFile();
-
- theDialog->Show();
-
- LStyledTextEdit* theField = (LStyledTextEdit*) mDialog->FindPaneByID(kTheTextET);
- SignalIf_(theField == nil);
- theDialog->SetLatentSub(theField);
- theField->SelectNone();
-
-
- SInt32 theResult = -1;
-
- while (true) {
- MessageT hitMessage = DoDialog();
-
- if (hitMessage == msg_Cancel) {
- theResult = 1;
- break;
-
- } else if (hitMessage == kRemoveTextB) {
-
- Handle theRsrcHandle = Get1Resource( 'TEXT', mTextRsrcID );
- if( theRsrcHandle ) {
- RemoveResource( theRsrcHandle );
- DisposeHandle( theRsrcHandle );
- }
- theRsrcHandle = Get1Resource( 'styl', mTextRsrcID );
- if( theRsrcHandle ) {
- RemoveResource( theRsrcHandle );
- DisposeHandle( theRsrcHandle );
- }
- mTextRsrcID = 0;
- theResult = 2;
- break;
-
-
- } else if (hitMessage == msg_OK) {
- this->SaveWindowToFile();
- theResult = 0;
- break;
- }
- }
-
- return theResult;
- }
-
-
- void CTextEditorWindow::SaveWindowToFile()
- {
-
- Handle theTextRsrc = Get1Resource('TEXT', mTextRsrcID );
- RemoveResource( theTextRsrc );
- DisposeHandle( theTextRsrc );
-
- Handle theStyleRsrc = Get1Resource('styl', mTextRsrcID );
- RemoveResource( theStyleRsrc );
- DisposeHandle( theStyleRsrc );
-
- LStyledTextEdit* theField = (LStyledTextEdit*) mDialog->FindPaneByID(kTheTextET);
- SignalIf_(theField == nil);
-
- theTextRsrc = theField->GetTextHandle();
- HandToHand( &theTextRsrc );
- AddResource( theTextRsrc, 'TEXT', mTextRsrcID, "\p" );
-
- theStyleRsrc = (Handle)theField->GetStyleScrapHandle();
- HandToHand( &theStyleRsrc );
- AddResource( theStyleRsrc, 'styl', mTextRsrcID, "\p" );
-
- UpdateResFile( mFileRefNum );
- }
-
-
- void CTextEditorWindow::LoadWindowFromFile()
- {
-
- Handle theTextRsrc = Get1Resource('TEXT', mTextRsrcID );
- Handle theStyleRsrc = Get1Resource('styl', mTextRsrcID );
-
- // Set File Path name
- LStyledTextEdit* theField = (LStyledTextEdit*) mDialog->FindPaneByID(kTheTextET);
- SignalIf_(theField == nil);
-
- if( theTextRsrc )
- theField->SetTextHandle( theTextRsrc, (TEStyleHandle)theStyleRsrc );
-
- }
-
-
-
-